﻿// Decompiled with JetBrains decompiler
// Type: Bannerlord.ModuleManager.CollectionsExtensions
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections.Generic;

#nullable enable
namespace Bannerlord.ModuleManager
{
  internal static class CollectionsExtensions
  {
    public static int IndexOf<T>(this IReadOnlyList<T> self, T elementToFind)
    {
      int num = 0;
      foreach (T objA in (IEnumerable<T>) self)
      {
        if (object.Equals((object) objA, (object) elementToFind))
          return num;
        ++num;
      }
      return -1;
    }

    public static int IndexOf<T>(this IReadOnlyList<T> self, Func<T, bool> preficate)
    {
      int num = 0;
      foreach (T obj in (IEnumerable<T>) self)
      {
        if (preficate(obj))
          return num;
        ++num;
      }
      return -1;
    }

    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
      this IEnumerable<TSource> source,
      Func<TSource, TKey> keySelector)
    {
      return source.DistinctBy<TSource, TKey>(keySelector, (IEqualityComparer<TKey>) null);
    }

    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
      this IEnumerable<TSource> source,
      Func<TSource, TKey> keySelector,
      IEqualityComparer<TKey>? comparer)
    {
      return CollectionsExtensions.DistinctByIterator<TSource, TKey>(source, keySelector, comparer);
    }

    private static IEnumerable<TSource> DistinctByIterator<TSource, TKey>(
      IEnumerable<TSource> source,
      Func<TSource, TKey> keySelector,
      IEqualityComparer<TKey>? comparer)
    {
      using (IEnumerator<TSource> enumerator = source.GetEnumerator())
      {
        if (enumerator.MoveNext())
        {
          HashSet<TKey> set = new HashSet<TKey>(comparer);
          do
          {
            TSource element = enumerator.Current;
            if (set.Add(keySelector(element)))
              yield return element;
            element = default (TSource);
          }
          while (enumerator.MoveNext());
          set = (HashSet<TKey>) null;
        }
      }
    }
  }
}
